package com.wangshb.upms.controller;

import com.wangshb.upms.core.tree.TreeNode;
import com.wangshb.upms.dto.upms.rolePermission.UpmsAllocatePermissionRequestDto;
import com.wangshb.upms.dto.upms.rolePermission.UpmsRolePermissionGetRequestDto;
import com.wangshb.upms.dto.upms.rolePermission.UpmsRolePermissionGetResponseDto;
import com.wangshb.upms.service.UpmsPermissionService;
import com.wangshb.upms.service.UpmsRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author auto code
 */
@RestController
public class UpmsRolePermissionController {

    @Autowired
    private UpmsRolePermissionService upmsRolePermissionService;

    @Autowired
    private UpmsPermissionService upmsPermissionService;


    @PostMapping(value = "/upms/role/permission/get")
    public UpmsRolePermissionGetResponseDto treePermissionByRoleId(@RequestBody UpmsRolePermissionGetRequestDto getRequestDto) {

        TreeNode treeNode = TreeNode.listToTree(this.upmsPermissionService.findForPermissionTree());
        List<Long> checkedRoleIds = this.upmsRolePermissionService.listPermissionByRoleId(getRequestDto.getUpmsRoleId());
        return UpmsRolePermissionGetResponseDto.builder().checkedKeys(checkedRoleIds).treeNode(treeNode).build();
    }


    @PostMapping(value = "/upms/role/permission/allocate")
    public void allocatePermissionByRoleId(@RequestBody UpmsAllocatePermissionRequestDto allocateRoleRequestDto) {
        this.upmsRolePermissionService.allocatePermissions(allocateRoleRequestDto.getUpmsRoleId(), allocateRoleRequestDto.getPermissionIdList());
    }
}
